package com.onluy.provider.department.conf;

import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import java.util.Enumeration;
import java.util.Set;

import static org.springframework.http.HttpStatus.NOT_EXTENDED;

/**
 * Created by wuwf on 17/3/31.
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    protected Logger log =   LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 在controller里面内容执行之前，校验一些参数不匹配啊，Get post方法不对啊之类的
     */
    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
        log.error("------------------GlobalExceptionHandler.handleExceptionInternal---------------------");
        RspInfo jsonResult = new RspInfo();
        jsonResult.setRspDesc(ex.getMessage());
        jsonResult.setRspCode(-9999);
        return new ResponseEntity<Object>(jsonResult, NOT_EXTENDED);

    }

    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handle(HttpServletRequest request, Exception e) {
        log.error("------------------GlobalExceptionHandler.handle---------------------");
        StringBuilder errmsg=new StringBuilder();
        if(e instanceof ConstraintViolationException){
//            ConstraintViolationException exs = (ConstraintViolationException) e;
//
//            Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
//            for (ConstraintViolation<?> item : violations) {
//                errmsg.append(",");
//                errmsg.append(item.getMessage()) ;
//            }
            errmsg.append(e.getMessage());
        }
        else{
//            errmsg.append(e.getClass().getName());
//            errmsg.append(":");
            errmsg.append(e.getMessage());
        }
        e.printStackTrace();
        return errmsg.toString() ;
    }


    private void log(Exception ex, HttpServletRequest request) {
        logger.error("************************异常开始*******************************");
        logger.error("请求异常："+ex);
        logger.error("请求地址：" + request.getRequestURL());
        Enumeration enumeration = request.getParameterNames();
        logger.error("请求参数");

        StackTraceElement[] error = ex.getStackTrace();
        for (StackTraceElement stackTraceElement : error) {
            logger.error("==============="+stackTraceElement.toString());
        }
        logger.error("************************异常结束*******************************");
    }
}
